package it.tcm.TeacherClassMate.facade;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import it.tcm.TeacherClassMate.beans.TransactionBean;
import it.tcm.TeacherClassMate.beans.UserBean;
import it.tcm.TeacherClassMate.dao.TransactionDao;
import it.tcm.TeacherClassMate.persistent.HibernateUtil;


public class TransactionManageBase {

	 	private Logger logger = Logger.getLogger(this.getClass().getName());   
	    private TransactionDao tDao = new TransactionDao();
	  
	    public TransactionBean getTransactionById(Integer id) {
		    Session session = null;
		    TransactionBean t = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				tDao.setSession(session);
				t = tDao.findById(id, false);
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading transaction "  + id.toString(), e);
			} finally {
			}
			return t;
	    }
	    
	   
	    
	    public Integer save(TransactionBean t)
	    {
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  		tDao.setSession(session);
	  		tDao.attachDirty(t);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  logger.error("Errore in save transaction ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return t.getId();
	    }
	    
	    
	    public boolean delete(TransactionBean t) {  
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  	    tDao.setSession(session);
	  		tDao.delete(t);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  logger.error("Errore in delete trasaction ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return true;
	    }
	    
	    public List<TransactionBean> getAll() {
		    Session session = null;
		    List<TransactionBean> tList = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				tDao.setSession(session);
				tList = tDao.findAll();
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Errore in reading all trasanctions beans ", e);
			} finally {
			}
			return tList;
	    }
	   
	    public List<TransactionBean> getTransactionBeanBytransactId(String trasact) {
	    	Session session = null;
	    	List<TransactionBean> tList = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("id"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion c = Restrictions.eq("transactId", trasact).ignoreCase();
				 criterion.add(c);				
				 tDao.setSession(session);
				 tList = tDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading transaction by trasact id", e);
			}
			return tList;
	    }	
	    
	    public List<TransactionBean> getTransactionBeanByuserAndTtransactId(Integer userid, String trasact) {
	    	Session session = null;
	    	List<TransactionBean> tList = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("id"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion c = Restrictions.eq("transactId", trasact).ignoreCase();
				 criterion.add(c);
				 Criterion c2 = Restrictions.eq("userId", userid);
				 criterion.add(c2);
				 tDao.setSession(session);
				 tList = tDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading transaction by trasact e userid", e);
			}
			return tList;
	    }	
	    
	    
	    public List<TransactionBean> getTransactionBeanByUser(Integer userid) {
	    	Session session = null;
	    	List<TransactionBean> tList = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("id"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion c = Restrictions.eq("userId", userid);
				 criterion.add(c);
				 tDao.setSession(session);
				 tList = tDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading transaction by userid", e);
			}
			return tList;
	    }	
	    

}
